import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/home/index.vue'
import HomeIndex from '@/views/home/home.vue'
import Login from '@/views/login/index.vue'
import Regins from '@/views/regins/index.vue'
import BackPass from '@/views/backPass/index.vue'
import Newblog from '@/views/newblog/index.vue'
import Memberinfo from '@/views/memberinfo/index.vue'

const witerList = ['/memberinfo']

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Home,
      children: [
        {
          path: '',
          name: 'home',
          component: HomeIndex
        },
        {
          path: 'newblog',
          name: 'newblog',
          component: Newblog
        },
        {
          path: '/memberinfo',
          component: Memberinfo
        },
      ]
    },
    {
      path: '/backPass',
      component: BackPass
    },
    {
      path: '/regins',
      component: Regins
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/404/index.vue')
    },
  ]
})

router.beforeEach(to => {
  const getItem =  localStorage.getItem('realname.id')
  const info = JSON.parse(getItem || '{}')
  const {fullPath} = to
  if(witerList.includes(fullPath.split('?')[0]) && !info?.realname){
    return { name: '404' }
  }
  return true
})
export default router
